//index.js
//获取应用实例
const app = getApp()

Page({
    data: {
		keyword: '',
		brandId: '',
		categoryId: '',
		genderId: '',
		sort: '',
		products: [],
		sortType: '',
		sortTNavType: '',
		categoryList: [],
		brandList: [],
		genderList: [],
		sortList: ['Low to High', 'High to Low'],
		curTitle: '',
    },
	changeType: function (e) {
    	if(e.currentTarget.dataset.type == this.data.sortType) {
			this.setData({
				sortType: ''
			})
		} else {
			this.setData({
				sortType: e.currentTarget.dataset.type
			})
		}
	},
	selectCurIndex: function (e) {
		this.setData({
			curTitle: e.currentTarget.dataset.itemIndex,
			sortType: '',
			sortTNavType:  e.currentTarget.dataset.type,
			brandId : e.currentTarget.dataset.brandId ? e.currentTarget.dataset.brandId : '',
			categoryId: e.currentTarget.dataset.categoryId ? e.currentTarget.dataset.categoryId : '',
			genderId: e.currentTarget.dataset.genderId ? e.currentTarget.dataset.genderId : '',
			sort: e.currentTarget.dataset.sort ? e.currentTarget.dataset.sort : ''
		});
		var that = this;
		wx.request({
			url: 'http://192.168.1.199:3389/product/list',
			data: {
				keyword: that.data.keyword,
				brand_id : that.data.brandId,
				category_id: that.data.categoryId,
				gender_id: that.data.genderId,
				sort: that.data.sort
			},
			success: function (res) {
				that.setData({
					products: res.data,
				})
			}
		});
		wx.setNavigationBarTitle({
			title: that.data.curTitle ? that.data.curTitle : 'category'
		});
	},
    onLoad: function () {
		var that = this;
		wx.request({
			url: 'http://192.168.1.199:3389/Category',
			data: {type : 'category'},
			success: function (res) {
				that.setData({
					categoryList: res.data
				})
			}
		});
		wx.request({
			url: 'http://192.168.1.199:3389/Category',
			data: {type : 'brand'},
			success: function (res) {
				that.setData({
					brandList: res.data
				})
			}
		});
		wx.request({
			url: 'http://192.168.1.199:3389/Category',
			data: {type : 'gender'},
			success: function (res) {
				that.setData({
					genderList: res.data
				})
			}
		});
		this.setData({
			productBaseUrl: app.globalData.productBaseUrl
		});
    },
	onShow: function() {
		if(app.brandId) {
			this.setData({
				brandId: app.brandId ? app.brandId : '',
				curTitle: app.curTitle ? app.curTitle : '',
				sortTNavType: 'brand',
				sortType: ''
			})
		}
		var that = this;
		wx.request({
			url: 'http://192.168.1.199:3389/product/list',
			data: {
				keyword: that.data.keyword,
				brand_id : that.data.brandId,
				category_id: that.data.categoryId,
				gender_id: that.data.genderId,
				sort: that.data.sort
			},
			success: function (res) {
				that.setData({
					products: res.data,
				})
			}
		});
		wx.setNavigationBarTitle({
			title: that.data.curTitle ? that.data.curTitle : 'category'
		});
    },
	onHide: function() {
	}
})
